package com.abc.robot01.activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import com.abc.robot01.R

/**
 * 方案720页面 加 户型选择
 */
class FangAnActivity : BaseVoiceActivity() {

    private lateinit var webView: WebView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val designPackageId = intent.getStringExtra("designPackageId");
        this.setContentView(R.layout.content_main5)
        webView = findViewById<WebView>(R.id.webView)
        webView.settings.javaScriptEnabled = true
        webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        webView.settings.domStorageEnabled = true
        // Ensure the WebView opens URLs in the WebView itself
        webView.webViewClient = WebViewClient()
        val renderUrl = intent.getStringExtra("renderUrl") + "&showType=5"
        val id = intent.getStringExtra("id")
        Log.e(TAG, "renderUrl:$renderUrl")
        webView.loadUrl(renderUrl!!)

        webView.addJavascriptInterface(WebAppInterface(context = this@FangAnActivity,designPackageId!!),"AndroidInterface")

        val c = findViewById<ComposeView>(R.id.compose_view)
        c.setContent {
            val scrollState = rememberScrollState()
            MaterialTheme {
                Box {
//                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
                    Row(modifier = Modifier.padding(30.px)) {
                        BackButton {
                        }
                    }

//                    OutlinedButton(
//                        onClick = {
//                            startActivity<HuXingXuanZheActivity>() {
//                                putExtra("designPackageId", designPackageId)
//                            }
//                        },
//                        border = BorderStroke(width = 1.dp, color = Color.White.copy(alpha = 0.5f)),
//                        contentPadding = PaddingValues(0.dp),
//                        modifier = Modifier
//                            .padding(0.dp)
//                            .offset(x = 482.dp, y = 224.dp)
//                            .size((52 * 3.5f).toInt().px, (28 * 3.5f).toInt().px),
//                    ) {
//                        Text(
//                            text = "户型选择",
//                            style = TextStyle(color = Color.White), fontSize = 30.mysp
//                        )
//                    }


                }
            }
        }
        Log.e(TAG, "onCreate");
    }

    // JS 调用的接口
    class WebAppInterface(private val context: Context,val designPackageId:String) {
        @JavascriptInterface
        fun import() {
            context.startActivity<HuXingXuanZheActivity>() {
                                putExtra("designPackageId", designPackageId)
                            }
            Log.e(HuXingTiaoZheng.TAG, "load:")
        }
    }

    override fun onPause() {
        super.onPause()
        webView.evaluateJavascript("javascript:closeMusic();", null)
    }

    override fun onResume() {
        super.onResume()
        webView.evaluateJavascript("javascript:openMusic();", null)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "onDestroy")
        webView.destroy()
    }

    companion object {
        val TAG = "FangAnActivity"
    }

}
